package com.xpalive.helper;

import com.xpalive.config.Property;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by xiongping on 2017/5/21.
 */
public class PropertiesReader {

    private static void load(String path) {
        List<String> regExList = new ArrayList<>();
        List<String> keyWordList = new ArrayList<>();
        Properties prop = new Properties();
        try {
            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
            prop.load(inputStream);
            for (Object o : prop.keySet()) {
                if("root".equals(o)){
                    Property.setRoot((String) prop.get(o));
                }else if("notFoundPageUrl".equals(o)){
                    Property.setNotFoundPageUrl((String) prop.get(o));
                }else if(((String)o).startsWith("regEx.")){
                    regExList.add((String) prop.get(o));
                }else if(((String)o).startsWith("keyWord.")){
                    keyWordList.add((String) prop.get(o));
                }
            }
            Property.setRegExList(regExList);
            Property.setKeyWordList(keyWordList);
        } catch (IOException e) {
            throw new IllegalArgumentException("属性文件加载失败");
        }
    }

    public static void load() {
        load("config.properties");
    }
}
